package com.example.mytestdemo.threejs

import android.util.Log
import android.webkit.JavascriptInterface
import android.webkit.WebView
import android.webkit.WebViewClient
import com.example.mytestdemo.BaseViewBindActivity
import com.example.mytestdemo.R
import com.example.mytestdemo.databinding.ActivityThreejs3dLayoutBinding


/**
 *
 * 文本使用
 */
class ThreeJs3dActivity : BaseViewBindActivity<ActivityThreejs3dLayoutBinding>() {
    override fun getLayoutInflateId(): Int {
        return R.layout.activity_threejs_3d_layout
    }

    override fun initData() {

    }

    override fun initUI() {
        mBind.webView.getSettings().setJavaScriptEnabled(true)
        mBind.webView.setWebViewClient(WebViewClient())
        WebView.setWebContentsDebuggingEnabled(true);
//        mBind.webView.addJavascriptInterface(WebAppInterface(mBind.webView), "Android")
        mBind.webView.loadUrl("file:///android_asset/threejs_3d.html")


        // Example URLs
        val urls = arrayOf(
            "https://images.pexels.com/photos/24304598/pexels-photo-24304598.jpeg",
            "https://images.pexels.com/photos/16271119/pexels-photo-16271119.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2",
            "https://images.pexels.com/photos/19835377/pexels-photo-19835377.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2",
            "https://images.pexels.com/photos/23879112/pexels-photo-23879112.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2"
        )


        // Call JavaScript function to set texture URLs
//        mBind.webView.post {
//            mBind.webView.loadUrl("javascript:setImageUrl('" + urls[0] + "')")
//        }
    }
    class WebAppInterface(val webView: WebView) {
        val urls = arrayOf(
            "https://picx.zhimg.com/70/v2-1dad14fb24aa9ea316db5a3c70c75db5_1440w.avis?source=172ae18b&biz_tag=Post",
            "https://images.pexels.com/photos/24304598/pexels-photo-24304598.jpeg",
            "https://images.pexels.com/photos/16271119/pexels-photo-16271119.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2",
            "https://images.pexels.com/photos/19835377/pexels-photo-19835377.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2",
            "https://images.pexels.com/photos/23879112/pexels-photo-23879112.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2"
        )

        @JavascriptInterface
        fun setImageUrl(url: kotlin.String) {
            Log.d(TAG, "tgw setImageUrl:$url ")
            webView.post { webView.loadUrl("javascript:setImageUrl('$url')") }
        }
       @JavascriptInterface
        fun frontImageUrl(): kotlin.String {
           Log.d("tgw", "frontImageUrl: ")
           return urls[0]
       }

        @JavascriptInterface
        fun backImageUrl(): kotlin.String{
           return urls[1]
        }

        @JavascriptInterface
        fun leftImageUrl(): kotlin.String{
            return urls[2]
        }


        @JavascriptInterface
        fun rightImageUrl(): kotlin.String{
            return urls[3]
        }

    }

}